package com.xinz.protocal.client;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public interface RemoteInvokeListener {

	void onBeginInvoke(Method method, Object[] args);
	
	void onInvokeSuccess(Object header, Object result);
	
	void onInvokeException(Object header, Throwable e);
	
	public static class Abstract implements RemoteInvokeListener {

		@Override
		public void onBeginInvoke(Method method, Object[] args) {
			
		}

		@Override
		public void onInvokeSuccess(Object header, Object result) {
			
		}

		@Override
		public void onInvokeException(Object header, Throwable e) {
			
		}
		
	}
	
	public static class ListListener implements RemoteInvokeListener {

		private List<RemoteInvokeListener> listeners = new ArrayList<RemoteInvokeListener>();
		
		@Override
		public void onBeginInvoke(Method method, Object[] args) {
			for(int i = 0; i < listeners.size(); i++) {
				listeners.get(i).onBeginInvoke(method, args);
			}
		}

		@Override
		public void onInvokeSuccess(Object header, Object result) {
			for(int i = 0; i < listeners.size(); i++) {
				listeners.get(i).onInvokeSuccess(header, result);
			}
		}

		@Override
		public void onInvokeException(Object header, Throwable e) {
			for(int i = 0; i < listeners.size(); i++) {
				listeners.get(i).onInvokeException(header, e);
			}
		}
		
		public void add(RemoteInvokeListener listener) {
			listeners.add(listener);
		}
		
	}
}
